<?php
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

/* @var $model \yii\db\ActiveRecord */
$model          = new $generator->modelClass();
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
	$safeAttributes = $model->attributes();
}

$cols = [];

foreach ($generator->getColumnNames() as $attribute) {
	if (in_array($attribute, $safeAttributes)) {
		$cols[] = [
			'field' => $attribute,
			'title' => $generator->getColumnLabel($attribute),
		];
	}
}

$class = $generator->modelClass;
$pk    = $class::primaryKey();
if (is_array($pk) && count($pk) >= 1) {
	$pk = $pk[0];
}
if (empty($pk)) {
	$pk = 'id';
}

echo "<?php\n";
?>
use yii\web\View;
use yii\helpers\Url;

/* @var $this yii\web\View */

$pk =<?= var_export($pk, true); ?>;

$opts = json_encode([
'pk' => $pk,
'dataUrl' => Url::to(['index', '_query'=>1]),
'createUrl' => Url::to(['create']),
'saveUrl' => Url::to(['save']),
'viewUrl' => Url::to(['view']),
'deleteUrl' => Url::to(['delete']),
], JSON_UNESCAPED_UNICODE);

\common\widgets\JsBlock::begin();
<?php
echo "?>\n";
?>
<script type="application/javascript">
	var opts = <?php echo '<?=$opts?>';?>;

	$(function () {

		$('#dg-listview').datagrid({
			singleSelect: true,
			method: 'get',
			rownumbers: true,
			url: opts.dataUrl,
			border: false,
			fit: true,
			striped: true,
			multiSort: true,
			remoteSort: true,
			toolbar: '#dg-toolbar',
			footer: '#ft',
			idField: "<?='<?=$pk?>'?>",
			columns: [[
<?php
				foreach($cols as $col) {
					echo str_repeat("\t", 4).json_encode($col, JSON_UNESCAPED_UNICODE) . ',' . PHP_EOL;
				} ?>
			]],
			pagination: true,
			//toolbar: [{
			//	iconCls: 'icon-edit',
			//	handler: function(){alert('edit')}
			//},'-',{
			//	iconCls: 'icon-help',
			//	handler: function(){alert('help')}
			//}],
			loader: function (param, success_callback, error_callback) {
				return datagrid_loader({
					url: opts.dataUrl,
					param: param,
					success_callback: success_callback,
					error_callback: error_callback
				});
			}
		});
	});
</script>
<?php
echo "<?php\n";
?>
\common\widgets\JsBlock::end();
$this->registerJs($this->render('js/script.js'));
<?php
echo "?>\n";
?>

<table id="dg-listview" style="width: 100%;" toolbar="#dg-toolbar">

</table>

<div id="dg-toolbar">
	<a id="btn-new" iconCls="icon-add">增加</a>
	<a id="btn-edit" iconCls="icon-edit">修改</a>
	<a id="btn-view" iconCls="icon-magnifier">查看</a>
	<a id="btn-delete" iconCls="icon-remove">删除</a>
	<span><input id="inp-search"></span>
</div>
